package com.shanghaionstar.discover.violation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.mag.thirds.VehicleViolationBean;
import com.onstar.cn.weight.DeleteEditText;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.iview.IVehicleViolationView;
import com.shanghaionstar.discover.presenter.VehicleViolationPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.PlateNumberProvinceLayout;

/**
 * Created by 7828wc on 16/7/19.
 */
@ContentView(R.layout.discover_vehicle_violation)
public class VehicleViolationActivity extends SwipeActivity implements IVehicleViolationView {

    @FindViewById
    private TextView vehicle_vin, tv_plate_number_province;
    @FindViewById
    private DeleteEditText et_plate_number, et_engine_number;
    @FindViewById
    private ProgressLayout progress_layout;
    @FindViewById
    private CommonProgressButton btn_query;
    @FindViewById
    private PlateNumberProvinceLayout layout_bottom_pop;

    private VehicleViolationPresenter presenter;
    private boolean isQueryViolation = false;
    /**
     * 标示是否自动查询结果跳转结果页面，自动查询需强制关闭当前页面
     */
    private boolean isAutoJump = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        vehicle_vin.setText(CommonUtil.maskWithStar(ApplicationUtil.getInstance().getVin(), 3, 4));

        setListener();

        progress_layout.setVisibility(View.VISIBLE);
        isQueryViolation = false;
        presenter = new VehicleViolationPresenter(getScheduler());
        presenter.setView(this);
        presenter.updateVehicleInfo(null, null, true);
        presenter.start();

        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (presenter.verifyPlateNumber(tv_plate_number_province.getText().toString() + et_plate_number.getText().toString(), true)
                        && presenter.verifyPlateNumber(et_engine_number.getText().toString(), false)) {
                    isQueryViolation = true;
                    setLoading(true);
                    isAutoJump = false;
                    PlateEngineNumEntity engineNumEntity = new PlateEngineNumEntity();
                    engineNumEntity.setEngineNumber(et_engine_number.getText().toString());
                    engineNumEntity.setLicensePlate(tv_plate_number_province.getText().toString() + et_plate_number.getText().toString());
                    engineNumEntity.setVin(ApplicationUtil.getInstance().getVin());
                    engineNumEntity.setUpdate(true);
                    presenter.voilationQuery(engineNumEntity);
                }
            }
        });

    }

    private void setListener() {
        layout_bottom_pop.setPopItemClickListener(new PlateNumberProvinceLayout.PopItemClickListener() {
            @Override
            public void onPopItemClickListener(String plateType) {
                presenter.popAnimationWithKeyboard(false);
                tv_plate_number_province.setText(plateType);
            }
        });
    }

    @OnClick({R.id.tv_plate_number_province})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_plate_number_province:
                if (layout_bottom_pop.getVisibility() == View.VISIBLE) {
                    layout_bottom_pop.setVisibility(View.GONE);
                } else {
                    layout_bottom_pop.setVisibility(View.VISIBLE);
                }
                presenter.popAnimationWithKeyboard(layout_bottom_pop.getVisibility() == View.VISIBLE);
                CommonUtil.hideKeyBoard(this, et_plate_number);
                break;
            default:
                break;
        }
    }

    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    public void setLoading(boolean isLoading) {
        et_plate_number.setEnabled(!isLoading);
        et_engine_number.setEnabled(!isLoading);
        tv_plate_number_province.setEnabled(!isLoading);
        if (isQueryViolation) {
            btn_query.showLoading(isLoading);
//        } else {
//            progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        progress_layout.setVisibility(View.GONE);

        btn_query.showLoading(false);
        setLoading(false);
    }

    @Override
    public void onReponseSuccess(Object object) {
        if (object != null) {
            if (object instanceof PlateEngineNumEntity) {
                PlateEngineNumEntity plageEngineNumber = (PlateEngineNumEntity) object;
                if (!CommonUtil.isNull(plageEngineNumber.getLicensePlate())) {
                    tv_plate_number_province.setText(plageEngineNumber.getLicensePlate().substring(0, 1));
                    et_plate_number.setText(plageEngineNumber.getLicensePlate().substring(1));
                }
                et_engine_number.setText(plageEngineNumber.getEngineNumber());

                if (!CommonUtil.isNull(plageEngineNumber.getLicensePlate()) && !CommonUtil.isNull(plageEngineNumber.getEngineNumber())) {
                    isQueryViolation = true;
                    plageEngineNumber.setUpdate(false);
                    presenter.voilationQuery(plageEngineNumber);
                    isAutoJump = true;
                } else {
                    progress_layout.setVisibility(View.GONE);
                    isQueryViolation = false;
                }
            } else if (object instanceof VehicleViolationBean) {
                isQueryViolation = false;
                setLoading(false);
                Intent intent = new Intent(onActivity(), ViolationResultActivity.class);
                intent.putExtra("violation", (VehicleViolationBean) object);
                startActivity(intent);
                if (isAutoJump) {
                    finish();
                }
            }
        }
    }

    @Override
    public void popAnimationWithKyeboard(Animation animation, boolean isOpen) {
        layout_bottom_pop.clearAnimation();
        int height = layout_bottom_pop.getMeasuredHeight();
        ViewGroup.MarginLayoutParams bottomMargins = (ViewGroup.MarginLayoutParams) layout_bottom_pop.getLayoutParams();
        bottomMargins.bottomMargin = isOpen ? 0 : -height;
        layout_bottom_pop.setLayoutParams(bottomMargins);
        layout_bottom_pop.setAnimation(animation);
        layout_bottom_pop.startAnimation(animation);
    }

    @Override
    public Activity onActivity() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommonUtil.hideKeyBoard(this, et_plate_number);
    }
}
